<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Classifieds extends MY_Controller {

    public function index() {
        
        $data['logged_in']  = $this->logged_in;
        $data['is_admin']   = $this->is_admin;
        $data['first_name'] = $this->first_name;

        
        $data['title'] = "Central Alabama VW Club -- Club";
        $data['heading'] = "Classifieds";
		
		$this->load->library('table');
		$this->table->set_heading('Image','Nickname','Year','Model','Engine','Price','Seller');
		$this->load->model('Classified_model');
		$classified_list = $this->Classified_model->Get_classifieds();
		
		//loop through the results and checkbox the id
		foreach($classified_list as $row)
		{
            $row['nickname'] = anchor(base_url().'index.php?/classifieds/view_classified/'.$row['car_id'],($row['nickname'] == '' ? 'No NickName' : $row['nickname']));
            unset($row['car_id']); 
			$this->table->add_row($row);
		}
		
		$local_table_template = set_table_template();
		$this->table->set_template($local_table_template); 
		$local_table = $this->table->generate();
		$data['classified_list'] = $local_table;
        //stop

        $data['view'] = 'classifieds_view';
        $this->load->vars($data);
        $this->load->view('master_view', $data);
    }
    
    function view_classified() {
        define("CAR_SEGMENT", 3);
        
        
        //Load_classified
        $basic_data['first_name'] = $this->first_name;
        $basic_data['save_type'] = 'view';
		$basic_data['error'] = '';
        $basic_data['additional_javascript_references'] = '<script type="text/javascript" src="'.base_url().'js/member_car_form_functions.js"></script>
                                                           <script type="text/javascript" src="'.base_url().'js/jquery.colorbox-min.js"></script>
                                                          ';
        $basic_data['additional_css_references'] = link_tag('css/colorbox.css'.'?'.time());
        
        
        $car_id = $this->uri->segment(CAR_SEGMENT);
        
        $basic_data['additional_javascript_doc_ready_functions'] = "additional_doc_ready_code();";
      
        $form_basic_info    =   $this->_set_form_basics();
        $car_info           =   $this->_get_classified_information($car_id);
        $car_gallery        =   $this->_get_car_photos($car_id);
        		
        $data = array_merge($form_basic_info, $basic_data, $car_info, $car_gallery);
        
        $data['is_data_form']	= true;
        $data['title'] = 'Central Alabama VW Club -- View Car';
        $data['heading'] = 'Car Info';
        $data['return_location_html'] = 'classifieds';
        $data['return_location'] = 'Classified Listings';
        
        $data['view'] = 'car_profile_view';
        $this->load->vars($data);        
        $this->load->view('master_view', $data);
        return;
    }
    
	function _get_classified_information($car_id) {
		$this->load->model('Classified_model');
        
		$car_row = $this->Classified_model->Load_classified($car_id);
		
		//See if Array is nothing and no car returned
		if (count($car_row) == 0)
			redirect('classified');

		//Set the values for the Member Profile
        $ret_val['nickname']	=	$car_row->nickname;
		$ret_val['year']		=	$car_row->year;
		$ret_val['model']	=	$car_row->model;
		$ret_val['engine']	=	$car_row->engine;
		$ret_val['price']				=	$car_row->price;
		$ret_val['notes']				=	$car_row->notes;
        $ret_val['seller']				=	$car_row->seller;
        $ret_val['contact']				=	$car_row->contact;

        return $ret_val;
        
	}
    
    function _get_car_photos($car_id){
        
        $this->load->model('Car_model');
        
		$car_gallery_array = $this->Car_model->Get_car_gallery($car_id);
        
        $gallery_html   =   '';
        $gallery_html   .=  '<table class="car-gallery-grid">';
		       
		//loop through the results and checkbox the id
        $row_pic_count  =   0;
        $image_row      =   '';
        $start_count    =   false;
		foreach($car_gallery_array as $row) {
           if ($start_count)
            $row_pic_count++;
           else
            $start_count =   true;
           
           if ($row_pic_count == MAX_ROW_PICS) {
               $gallery_html .= '<tr>'.$image_row.'</tr>';
               $image_row = '';
               $row_pic_count = 0;
           }
               
           $thumb_link  = '<img src="./member_car_gallery/thumbnail/'.$row['pic_name'].'_thumb.jpg"/>';
           $image_link  = './member_car_gallery/'.$row['pic_name'].'.jpg';
           $full_link   = '<a class="carGallery"  href="'.$image_link.'" title="">'.$thumb_link.'</a>';
           $image_row   .=   '<td class="image-cell">'.$full_link.'</td>';
		}
		$gallery_html .= '<tr>'.$image_row.'</tr>';
        $gallery_html .= '</table>';
		$data['car_gallery'] = $gallery_html;
		
		return $data;
        
        return $car_gallery;
    }
    
    function _set_form_basics() {
		$ret_val = '';
		$ret_val['logged_in']  = $this->logged_in;
		$ret_val['is_admin']   = $this->is_admin;

		return $ret_val;
	}

}

/* End of file links.php */
/* Location: ./application/controllers/classifieds.php */